package com.cxs.controller;

import com.cxs.enums.HospitalErrorResultEnum;
import com.cxs.vo.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*
 * @Author:cxs
 * @Date:2021/10/15  15:29
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @PostMapping("/doLogin")
    public Result login(String loginName, String pwd) {
        System.out.println(loginName);
        System.out.println(pwd);
        if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(pwd)) {
            return Result.failare(HospitalErrorResultEnum.PARAMETERS_ERROR);
        }
        // 获取主体
        Subject subject = SecurityUtils.getSubject();

        subject.login(new UsernamePasswordToken(loginName, pwd));

        return Result.Ok(0, "登录成功", subject.getPrincipal());
    }


    @GetMapping("/logout")
    public Result logout() {
        Subject subject = SecurityUtils.getSubject();

        subject.logout();

        return Result.Ok();
    }
}
